package com.newcapec.servlet;

import com.newcapec.bean.UserEntity;
import com.newcapec.dao.SysUserDao;
import com.newcapec.dao.impl.SysUserDaoImpl;
import com.newcapec.service.SysUserService;
import com.newcapec.service.impl.SysUserServiceImpl;
import com.newcapec.util.MD5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Description:添加用户
 * @Author:yangshupeng
 * @Date: 2022/3/17 21:19
 * @Version:1.0
 */
@WebServlet("/addUser")
public class AddUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");*/

        String name = req.getParameter("name");
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        if (name != null && userName != null && password != null){
            password = MD5Util.toMD5(password);
            UserEntity userEntity = new UserEntity(name,userName,password);
            SysUserService sysUserService = new SysUserServiceImpl();
            if (sysUserService.addUser(userEntity)){
                resp.sendRedirect("AccountManagement.html");
            }
        }else {
            resp.getWriter().write("no");
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
